package com.pet.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.service.SysOrderService;
import com.pet.system.SysNotice;
import com.pet.system.SysOrder;
import com.pet.system.SysUser;
import com.pet.utils.Result;
import com.pet.vo.SysNoticeQueryVo;
import com.pet.vo.SysOrderQueryVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whd
 * @since 2023-08-04
 */
@RestController
@RequestMapping("/lovePet/system/sysOrder")
public class SysOrderController {


    @Autowired
    private SysOrderService sysOrderService;

    @GetMapping("/findAllOrder")
    public Result findAllOrder(){
        List<SysOrder> list = sysOrderService.list();
        return Result.ok(list);
    }


    //分页和条件查询
    @ApiOperation("分页和条件查询")
    @PostMapping("{page}/{limit}")
    public Result selectDataPageByVo(@PathVariable Long page,
                                     @PathVariable Long limit,
                                     @ApiParam("包装的查询分类名称名称") @RequestBody SysOrderQueryVo sysOrderQueryVo) {
//        System.out.println("=========" + sysNoticeQueryVo.getMessage());
        IPage<SysOrder> iPage = new Page<>(page, limit);
        iPage = this.sysOrderService.selectpage(iPage, sysOrderQueryVo);
        return Result.ok(iPage);
    }


    // 添加数据
    @PostMapping("/addData")
    public Result addData(@RequestBody SysOrder sysOrder) {
        sysOrder.setConsultationId(generateRandomString());
        boolean b = this.sysOrderService.save(sysOrder);
        return b ? Result.ok() : Result.fail();
    }

    //根据id获取数据
    @GetMapping("/findDataById/{id}")
    public Result findDataById(@PathVariable Long id) {
        SysOrder sysOrder = this.sysOrderService.getById(id);
        return Result.ok(sysOrder);
    }


    // 实现修改
    @PostMapping("/updateData")
    public Result updateData(@RequestBody SysOrder sysOrder) {
        boolean b = this.sysOrderService.updateById(sysOrder);
        return b ? Result.ok() : Result.fail();

    }

    @ApiOperation("根据id去移除一个宠数据")
    @DeleteMapping("/removeDataById/{id}")
    public Result removeDataById(@PathVariable Long id) {
        boolean b = this.sysOrderService.removeById(id);
        return b ? Result.ok() : Result.fail();
    }


    public String generateRandomString() {
        // 生成当前时间戳
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());

        // 随机数的位数
        int numDigits = 36 - timestamp.length();

        // 生成随机数
        Random random = new Random();
        int randomNum = random.nextInt((int) Math.pow(10, numDigits));

        // 拼接字符串
        String formattedRandomNum = String.format("%0" + numDigits + "d", randomNum);
        String result = timestamp + formattedRandomNum;

        return result;
    }


}

